package org.vartist.atld.menu.listener.main;

import java.util.ArrayList;

import org.vartist.atld.R;
import org.vartist.atld.menu.activity.main.MainActivity;
import org.vartist.atld.menu.consts.MenuConst;
import org.vartist.atld.menu.vo.MenuVO;
import org.vartist.atld.util.log.LogUtil;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class MenuOnItemClickListener implements OnItemClickListener {

	private Activity mActivity = null;
	private ArrayList<MenuVO> mMenuList = null;
	
	public MenuOnItemClickListener(Activity activity, ArrayList<MenuVO> menuList) {
		mActivity = activity;
		mMenuList = menuList;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		
		String activityName = null;
		
		/***
		 * get forward activity name
		 */
		if (mMenuList.size() > 0) {
			if (mMenuList.get(position) != null) {
				if (mMenuList.get(position).getMenuDept() > 0) {
					
					/***
					 * 1st dept of menu 
					 */
					if (mMenuList.get(position).getMenuDept() == MenuConst.CONST_MENU_DEPT_1st) {
						activityName = mMenuList.get(position).getForwardActivityName();
					}
					
					/***
					 * 2nd dept of menu
					 */
					else if (mMenuList.get(position).getMenuDept() == MenuConst.CONST_MENU_DEPT_2nd) {
						activityName = mMenuList.get(position).getForwardActivityName();
					}					
				}
			}
		}
		
		/***
		 * verify forward activity name
		 */
		if (activityName != null) {
			startForwardActivityByName(activityName);
		}
	}

	/**
	 * start activity by forward activity name in parameter
	 * 
	 * @param activityName
	 */
	private void startForwardActivityByName(String activityName) {
		try {
			Intent intent = new Intent(mActivity, Class.forName(activityName));
			mActivity.startActivity(intent);
		} catch (Exception e) {
			LogUtil.e(e.getMessage());
			Toast.makeText(mActivity, activityName + " " + mActivity.getString(R.string.menu_no_exist_message), Toast.LENGTH_SHORT).show();
		}
	}

}